#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <pthread.h>
#include<unistd.h>
#include <errno.h>
#include "proto.h"
#include "list.h"
#include "mlib.h"
#include "server.h"
#include "server.h"
#include<string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */


int num_list_entry;

static int tid_nextpos = 0;

struct thr_channel_entry_st
{
    chnid_t chnid;
    pthread_t tid;
};
struct thr_channel_entry_st thr_channel[CHNNR];

void *thr_channel_snder(void *ptr)
{
    struct msg_channel_st *sbufp;
    int len;
    struct msg_listentry_st *entry = ptr;
    sbufp = malloc(MSG_CHANNEL_MAX);
    if (sbufp == NULL)
    {
        syslog(LOG_ERR, "malloc():%s", strerror(errno));
        exit(1);
    }
    sbufp->chinid = entry->chnid; // 频道号处理
    printf("sbufp->chinid  %d\n", sbufp->chinid);
    
    // 频道内容读取
    while (1)
    {
        printf("channel  %d\n", __LINE__);
        len = mlib_readchn(entry->chnid, sbufp->data, 320 * 1024 / 8); // 320kbit/s   读取某一个频道的媒体文件
        syslog(LOG_DEBUG, "mlib_readchn() len: %d", len);
        if (len < 0)
        {
            break;
        }
        
        if (sendto(sfd, sbufp, len + sizeof(chnid_t), 0, (void *)&sndaddr, sizeof(sndaddr)) < 0)
        {
            syslog(LOG_ERR, "thr_channel(%d):sendto():%s", entry->chnid,
                   strerror(errno));
            break;
        }
        syslog(LOG_ERR, "thr_channel(%d):sendto():%s", entry->chnid,
                   strerror(errno));
        sched_yield(); //主动出让调度器
    }
    pthread_exit(NULL);
}
/* 每一个频道创建一个线程*/
int thr_channel_create(struct msg_listentry_st *list)
{
    int err;
     printf("channel   %d\n", __LINE__);
    syslog(LOG_DEBUG, "list content: chnid %d,  desc:%s\n", list->chnid, list->desc);
    err = pthread_create(&thr_channel[tid_nextpos].tid, NULL, thr_channel_snder, list);
    if (err)
    {
        syslog(LOG_ERR, "pthread_create():%s\n", strerror(errno));
        return -1;
    }
    thr_channel[tid_nextpos].chnid = list->chnid; //填写频道信息
    tid_nextpos++;
    return 0;
}

int thr_channel_destroy(struct mlib_listentry_st *ptr)
{
    int i;
    for (i = 0; i < CHNNR; i++)
    {
        if (thr_channel[i].chnid == ptr->chnid)
        {
            if (pthread_cancel(thr_channel[i].tid) < 0)
            {
                syslog(LOG_ERR, "pthread_cancel():thr thread of channel%d", ptr->chnid);
                return -ESRCH;
            }
            pthread_join(thr_channel[i].tid, NULL);
            thr_channel[i].chnid = -1;
            break;
        }
    }
    return 0;
}
int thr_channel_destroyall(void)
{
    for (int i = 0; i < CHNNR; i++)
    {
        if (thr_channel[i].chnid > 0)
        {
            if (pthread_cancel(thr_channel[i].tid) < 0)
            {
                syslog(LOG_ERR, "pthread_cancel():thr thread of channel:%ld",
                       thr_channel[i].tid);
                return -ESRCH;
            }
            pthread_join(thr_channel[i].tid, NULL);
            thr_channel[i].chnid = -1;
        }
    }
    return 0;
}